import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_transaction_offer_type.dart';

abstract class GetGooglePlayPurchasedSubscriptionTransactionTime {
  DateTime call({
    required String purchaseItemSku,
    required DateTime expirationTime,
    required GooglePlayPurchasedSubscriptionTransactionOfferType offerType,
  });
}

class GetGooglePlayPurchasedSubscriptionTransactionTimeImpl
    implements GetGooglePlayPurchasedSubscriptionTransactionTime {
  const GetGooglePlayPurchasedSubscriptionTransactionTimeImpl();

  @override
  DateTime call({
    required String purchaseItemSku,
    required DateTime expirationTime,
    required GooglePlayPurchasedSubscriptionTransactionOfferType offerType,
  }) {
    late final Duration duration;

    if (purchaseItemSku == 'com.meowofficial.words.month') {
      duration = const Duration(days: 30);
    } else if (purchaseItemSku == 'com.meowofficial.words.quarter') {
      duration = const Duration(days: 90);
    } else if (purchaseItemSku == 'com.meowofficial.words.year') {
      duration = const Duration(days: 365);
    } else if (purchaseItemSku == 'com.meowofficial.words.year.introduction') {
      duration = const Duration(days: 365);
    } else if (purchaseItemSku == 'com.meowofficial.words.2.month') {
      duration = const Duration(days: 30);
    } else if (purchaseItemSku == 'com.meowofficial.words.3.month.trial') {
      if (offerType.isTrialPeriod) {
        duration = const Duration(days: 3);
      } else {
        duration = const Duration(days: 30);
      }
    } else if (purchaseItemSku == 'com.meowofficial.words.3.year.trial') {
      if (offerType.isTrialPeriod) {
        duration = const Duration(days: 3);
      } else {
        duration = const Duration(days: 365);
      }
    }

    return expirationTime.subtract(duration);
  }
}
